package ATBMS.control;

import ATBMS.modles.DAO.DAOFactory;
import ATBMS.modles.DAO.UserDAO;
import ATBMS.modles.User;
import ATBMS.view.admin.AdminViewInterface;
import java.awt.event.ActionEvent;
import java.util.List;

/**
 * Administrator Controller
 */
public class AdminControl {

    private AdminViewInterface av;

    public void setAdminView(AdminViewInterface av) {
        this.av = av;

        refreshManagerTable();
    }

    private void refreshManagerTable() {
        UserDAO userdao = DAOFactory.getUserDAO();
        if (userdao == null) {
            av.alert("Can't Connect To DB!");
            return;
        }
        List<User> users = userdao.findByLevel(User.USERLEVEL_MANAGER);
        av.listUser(users);
    }

    /**
     * Creates a new <code>AdminControl</code> object 
     */
    public AdminControl() {
    }

    /**
     * Create a Action For AdminView
     * @return javax.swing.Action for adminstrtor menu
     */
    public javax.swing.Action getMenuAction() {
        return new javax.swing.AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                if (e.getActionCommand().compareTo("New Manager") == 0) {
                    User newUser = av.showNewManagerDialog();
                    if (newUser != null) {

                        UserDAO userdao = DAOFactory.getUserDAO();
                        if (userdao == null) {
                            av.alert("Can't Connect To DB!");
                            return;
                        }
                        if (userdao.insert(newUser)) {
                            refreshManagerTable();
                        }
                    }
                } else if (e.getActionCommand().compareTo("Edit Manager") == 0) {
                    String managerName = av.getSelectedManagerName();
                    if (managerName != null && managerName.length() != 0) {
                        UserDAO userdao = DAOFactory.getUserDAO();
                        if (userdao == null) {
                            av.alert("Can't Connect To DB!");
                            return;
                        }
                        User user;
                        if ((user = userdao.findByName(managerName)) != null) {
                            int uid = user.getUid();
                            user = av.showEditManagerDialog(user);
                            if (user != null) {
                                if (userdao.update(uid, user)) {
                                    refreshManagerTable();
                                }
                            }
                        }
                    } else {
                        av.alert("Please select a manager!");
                    }

                } else if (e.getActionCommand().compareTo("Delete Manager") == 0) {
                    String managerName = av.getSelectedManagerName();
                    if (managerName != null && managerName.length() != 0) {
                        UserDAO userdao = DAOFactory.getUserDAO();
                        if (userdao == null) {
                            av.alert("Can't Connect To DB!");
                            return;
                        }
                        User user;
                        if ((user = userdao.findByName(managerName)) != null) {
                            userdao.delete(user.getUid());
                            refreshManagerTable();
                        }
                    } else {
                        av.alert("Please select a manager!");
                    }
                }

            }
        };
    }
}
